package com.zhu.storesmanager;

import java.util.ArrayList;
import java.util.List;

import com.zhu.storesmanager.models.BrandsDataItem;
import com.zhu.storesmanager.models.CompeteProductItem;
import com.zhu.storesmanager.models.DataItem;
import com.zhu.storesmanager.models.ProjectDataItem;
import com.zhu.storesmanager.models.SyncModel;
import com.zhu.storesmanager.models.VisitTaskInfo;
import com.zhu.storesmanager.provider.StoresManagerSettings;
import com.zhu.storesmanager.utils.Actions;
import com.zhu.storesmanager.utils.DataUtils;
import com.zhu.storesmanager.utils.DatabaseUtil;
import com.zhu.storesmanager.utils.ViewsUtil;
import com.zhu.storesmanager.views.CompeteProductsView;
import com.zhu.storesmanager.views.NewProductsView;
import com.zhu.storesmanager.views.PopCompeteInfoView;
import com.zhu.storesmanager.views.PopMenu;
import com.zhu.storesmanager.views.PromotionsView;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 竞品信息
 */
public class SvStoreCompeteActivity extends Activity implements OnClickListener{

    private final static int SAVE_DIALOG = 0; //保存对话框

    public static final int TYPE_COMPETE_CONTENT_PRICE = 1; //类型－竞品价格
    public static final int TYPE_COMPETE_CONTENT_FUCTION = 2; //类型－新品功能
    public static final int TYPE_COMPETE_CONTENT_ACTIVITY = 3;//类型－竞品活动
    //异步查询数据库token
    private final static int DATAITEM_QUERY_TOKEN = 0; //查询所有数据词典
    private final static int SAVEDATA_QUERY_TOKEN = 1; //查询数据库保存的数据
    private final static int PROJECT_DATA_QUERY_TOKEN = 2; //拜访文本数据
    private final static int SELECT_QUERY_TOKEN = 3; //选中记录查询
    private DataQueryHandler mDataQueryHandler = null; //数据异步查询Handler

    private VisitTaskInfo mVisitTaskInfo = null; //拜访门店数据
    private CompeteProductsView mCompeteProductsView = null; //竞品产品价格
    private NewProductsView mNewProductsView = null; //新品上市信息
    private NewProductsView mPromotionsView = null; //新品活动信息
    private PopCompeteInfoView mPopCompeteInfoView = null;
    private SaveTask mSaveTask = null; //后台保存数据
    private PopMenu mPopMenu = null; //弹出菜单
    private boolean isDataChange = true; //数据是否发生变化
    private List<ProjectDataItem> mProjectList = null;
    private long mSelectedBdId = -1; //选择状态数据库ID


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parcelable parcelable = getIntent().getParcelableExtra("info");
        mVisitTaskInfo = (VisitTaskInfo) parcelable;
        setContentView(R.layout.activity_sv_storescompete);

        setupViews();
        initData();
    }


    /**
     * 初始化界面
     */
    private void setupViews(){
        ((TextView) findViewById(R.id.title_text)).setText(R.string.store_compete);
        findViewById(R.id.svbottom_menu_back).setOnClickListener(this);
        findViewById(R.id.svbottom_menu_save).setOnClickListener(this);
        findViewById(R.id.svbottom_menu_menu).setOnClickListener(this);
        mPopCompeteInfoView = (PopCompeteInfoView)LayoutInflater.from(this).inflate(R.layout.layout_popdialog_competeinfo, (ViewGroup)findViewById(R.id.layout), false);
        mPopCompeteInfoView.init((FrameLayout)findViewById(R.id.layout));
        mCompeteProductsView = (CompeteProductsView)findViewById(R.id.compete_products);
        mNewProductsView = (NewProductsView)findViewById(R.id.compete_newproducts);
        mNewProductsView.init(this, mPopCompeteInfoView, TYPE_COMPETE_CONTENT_FUCTION);
        mPromotionsView = (NewProductsView)findViewById(R.id.compete_promotions);
        mPromotionsView.init(this, mPopCompeteInfoView, TYPE_COMPETE_CONTENT_ACTIVITY);
        mPopMenu = new PopMenu(this, findViewById(R.id.bottom_menu), PopMenu.MENU_COMPETE);
        mPopMenu.setVisitTaskInfo(mVisitTaskInfo);
    }

    /**
     * 初始化界面数据
     */
    private void initData(){
    	if(!DataUtils.mUserData.isAdmin()){ //检测非管理员
    		if(mDataQueryHandler == null){
                mDataQueryHandler = new DataQueryHandler(getContentResolver()); 
            }
    		startQueryProjects();
    	}else{
    		setDataFromAdminSearchData();
    	}
    }

    /**
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v != null){
            switch(v.getId()){
                case R.id.svbottom_menu_back : //返回
                	 if(!doBack()){
                         finish();
                     }
                    break;
                case R.id.svbottom_menu_save: //保存
                    saveData();
                    break;
                case R.id.svbottom_menu_menu: //菜单
                	mPopMenu.showMenu();
                	break;
            }
        }
    }

    /**
     * 开始查询数据词典数据
     */
    private void startQueryDataItemList(){
        //查询指定类型的型号列表数据
        String where = StoresManagerSettings.DataDictionaryColumns.DATA_TYPE + "=" + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_BRANDS
                + " OR " + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE + "=" + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_MODELS
        + " OR " + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE + "=" + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_FUCTIONS
        + " OR " + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE + "=" + StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_ACTIVITYS;

        mDataQueryHandler.startQuery(DATAITEM_QUERY_TOKEN, null, 
                StoresManagerSettings.DataDictionaryColumns.CONTENT_URI_NO_NOTIFICATION,
                null, where, null, null);
    }

    /**
     * 开始查询竞品
     */
    private void startQueryCompeteData(){
        String where = StoresManagerSettings.SupervisorStoreCompeteColumns.STOREID + "=\'" + mVisitTaskInfo.mStoreId+ "\'";
        mDataQueryHandler.startQuery(SAVEDATA_QUERY_TOKEN, null, 
                StoresManagerSettings.SupervisorStoreCompeteColumns.CONTENT_URI_NO_NOTIFICATION,
                null, where, null, null);
    }

    /**
     * 查询project数据
     */
    private void startQueryProjects(){
    	String where = StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE + "=" + StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE;
    	mDataQueryHandler.startQuery(PROJECT_DATA_QUERY_TOKEN, null, 
				StoresManagerSettings.DataProjectsColumns.CONTENT_URI_NO_NOTIFICATION,
				null, where, null, null);
    }

    /**
     * 开始查询选中
     */
    private void startQuerySelectData(){
        String where = StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.STOREID + "=\'" + mVisitTaskInfo.mStoreId+ "\'";
        mDataQueryHandler.startQuery(SELECT_QUERY_TOKEN, null, 
                StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.CONTENT_URI_NO_NOTIFICATION,
                null, where, null, null);
    }

    /**
     * 异步数据查询
     */
    private final class DataQueryHandler extends AsyncQueryHandler {
        public DataQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        /**
         * 查询结果返回
         * (non-Javadoc)
         * @see android.content.AsyncQueryHandler#onQueryComplete(int, java.lang.Object, android.database.Cursor)
         */
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            switch (token) {
                case DATAITEM_QUERY_TOKEN:
                    updateDataItemFromCursor(cursor);
                    break;
                case SAVEDATA_QUERY_TOKEN:
                    updateDataFromCursor(cursor);
                    break;
                case PROJECT_DATA_QUERY_TOKEN:
               	   updateProjectDataFromCursor(cursor);
                   break;
                case SELECT_QUERY_TOKEN:
                	updateSelectItemFromCursor(cursor);
                	break;
                default:
                    Log.e("zhu", "onQueryComplete called with unknown token " + token);
            }
        }

        @Override
        protected void onDeleteComplete(int token, Object cookie, int result) {

        }
    }

    /**
     * 更新数据从数据库保存的数据
     * @param cursor 数据
     */
    private void updateDataFromCursor(Cursor cursor){
        try {

            final int brandIDIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_BRANDID);
            final int brandNameIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_BRANDNAME);
            final int dataIdsIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_DATAIDS);
            final int dataNamesIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_DATANAMES);
            final int modelIdIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_MODELID);
            final int modelNameIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_MODELNAME);
            final int otherIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_OTHERS);
            final int photoIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_PHOTO);
            final int priceIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_PRICE);
            final int sellcountIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_SELLCOUNT);
            final int typeIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteColumns.COMPETE_TYPE);
            final int savedIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SAVED);
            while (cursor.moveToNext()) {//遍历所有竞品数据
                CompeteProductItem item = new CompeteProductItem();
                item.mBrandId = cursor.getString(brandIDIndex);
                item.mBrandName = cursor.getString(brandNameIndex);
                item.mDataIDs = cursor.getString(dataIdsIndex);
                item.mDataNames = cursor.getString(dataNamesIndex);
                item.mModelID = cursor.getString(modelIdIndex);
                item.mModelName = cursor.getString(modelNameIndex);
                item.mOther = cursor.getString(otherIndex);
                item.mPrice = cursor.getString(priceIndex);
                item.mSellcount = cursor.getString(sellcountIndex);
                item.setImageData(cursor.getBlob(photoIndex));
                item.mType = cursor.getInt(typeIndex);
                boolean isSaved = cursor.getInt(savedIndex) == 1 ? true : false;
                ProjectDataItem pitem = null;
                if(item.mType == TYPE_COMPETE_CONTENT_PRICE){
                	pitem = DataUtils.getProjectDataItem(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE, 1);
                    if(isSaved){
                    	if(pitem != null && pitem.isSave && pitem.isShow){
                    		mCompeteProductsView.addCompeteProductData(item, true);
                    	}
                    }else{
                    	mCompeteProductsView.addCompeteProductData(item, true);
                    }
                }else if(item.mType == TYPE_COMPETE_CONTENT_FUCTION){
                	pitem = DataUtils.getProjectDataItem(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE, 2);
                    if(isSaved){
                    	if(pitem != null && pitem.isSave && pitem.isShow){
                    		mNewProductsView.addNewProductData(item);
                    	}
                    }else{
                    	mNewProductsView.addNewProductData(item);
                    }
                }else if(item.mType == TYPE_COMPETE_CONTENT_ACTIVITY){
                	pitem = DataUtils.getProjectDataItem(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE, 3);
                    if(isSaved){
                    	if(pitem != null && pitem.isSave && pitem.isShow){
                    		mPromotionsView.addNewProductData(item);
                    	}
                    }else{
                    	mPromotionsView.addNewProductData(item);
                    }
                }
            }
            //更新界面数据
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateDataFromCursor error");
        } finally {
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }
        startQuerySelectData();
    }

    /**
     * 更新选择栏数据词典数据
     */
    private void updateDataItemFromCursor(Cursor cursor){
        try {
            List<DataItem> brandsList = new ArrayList<DataItem>(); //竞品品牌
            List<DataItem> modelsList = new ArrayList<DataItem>(); //竞品型号
            List<DataItem> funtionsList = new ArrayList<DataItem>(); //竞品功能
            List<DataItem> activitysList = new ArrayList<DataItem>();  //竞品活动

            final int dataIDIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.DataDictionaryColumns._ID);
            final int dataNameIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.DataDictionaryColumns.DATA_NAME);
            final int dataTypeIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.DataDictionaryColumns.DATA_TYPE);
            while (cursor.moveToNext()) {//遍历所有型号数据
                DataItem item = new DataItem();
                item.mDataId = cursor.getString(dataIDIndex);
                item.mDataName = cursor.getString(dataNameIndex);
                item.mDataType = cursor.getInt(dataTypeIndex);
                if(StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_BRANDS == item.mDataType){
                    brandsList.add(item);
                }else if(StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_MODELS  == item.mDataType){
                    modelsList.add(item);
                }else if(StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_FUCTIONS  == item.mDataType){
                    funtionsList.add(item);
                }else if(StoresManagerSettings.DataDictionaryColumns.DATA_TYPE_COMPETE_ACTIVITYS == item.mDataType){
                    activitysList.add(item);
                }
            }
            //更新界面数据
            mCompeteProductsView.setChoiceviewList(brandsList, modelsList);
            mNewProductsView.setChoiceviewList(brandsList, funtionsList);
            mPromotionsView.setChoiceviewList(brandsList, activitysList);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateDataItemFromCursor error");
        } finally {
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }
        startQueryCompeteData();
    }


    /**
     * 更新选择栏数据词典数据
     */
    private void updateSelectItemFromCursor(Cursor cursor){
        try {
            final int dataIDIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns._ID);
            final int priceIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.PRICE);
            final int newIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.NEW);
            final int activityIndex = cursor.getColumnIndexOrThrow(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.ACTIVITY);
            if(cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()){
            	mSelectedBdId = cursor.getLong(dataIDIndex);
            	boolean priceSelect =  cursor.getInt(priceIndex) == 1 ? true : false;
            	boolean newSelect =  cursor.getInt(newIndex) == 1 ? true : false;
            	boolean activitySelect =  cursor.getInt(activityIndex) == 1 ? true : false;
            	  mCompeteProductsView.setSelectStatus(priceSelect);
                  mNewProductsView.setSelectStatus(newSelect);
                  mPromotionsView.setSelectStatus(activitySelect);
            }
          
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateDataItemFromCursor error");
        } finally {
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }
    }

    /**
     * (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(mNewProductsView.isTakePhoto()){
            mNewProductsView.onActivityResult(requestCode, resultCode, data);
        }else if(mPromotionsView.isTakePhoto()){
            mPromotionsView.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 按键事件处理
     */
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(!doBack()){
                finish();
            }
            return true;
        }
        return false;
    }

    /**
     * 返回事件处理
     */
    private boolean doBack(){
        if(mPopCompeteInfoView != null && mPopCompeteInfoView.doBack()){
            return true;
        }
        if(isDataChange){
        	if(DataUtils.mUserData.isAdmin()){
        		return false;
        	}
            popExitDialog();
            return true;
        }
        return false;
    }

    /**
     * 保存数据
     */
    private void saveData(){
    	if(!mNewProductsView.canSaveData() || !mPromotionsView.canSaveData()){ //检测能否保存数据
    		Actions.toastShort(this, R.string.save_fail_nophoto);
    		return;
    	}
        showDialog(SAVE_DIALOG);
        if(mSaveTask != null){
            mSaveTask.cancel(true);
            mSaveTask = null;
        }
        mSaveTask = new SaveTask();
        mSaveTask.execute();
    }

    /**
     * 登录进度提示
     */
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case SAVE_DIALOG:
                ProgressDialog pd = new ProgressDialog(this);
                pd.setMessage(this.getString(R.string.save_login)
                        + getString(R.string.please_wait));
                pd.setCancelable(false);
                pd.setCanceledOnTouchOutside(false);
                return pd;

            default:
                break;
        }
        return super.onCreateDialog(id);
    }

    /**
     * 保存数据线程
     */
    private class SaveTask extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            //清除所有数据
            String where = StoresManagerSettings.SupervisorStoreCompeteColumns.STOREID +"=\'" + mVisitTaskInfo.mStoreId +"\'";
            SvStoreCompeteActivity.this.getContentResolver().delete(StoresManagerSettings.SupervisorStoreCompeteColumns.CONTENT_URI_NO_NOTIFICATION, where, null);
            //保存数据
            mCompeteProductsView.saveData(mVisitTaskInfo.mStoreId);
            mNewProductsView.saveData(mVisitTaskInfo.mStoreId);
            mPromotionsView.saveData(mVisitTaskInfo.mStoreId);
            if(mCompeteProductsView.checkData(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE, 1))
            		&& mNewProductsView.checkData(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE, 2))
            		&& mPromotionsView.checkData(DataUtils.isProjectDataShow(mProjectList, StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE, 3))){
            	DatabaseUtil.saveVisitStatus(SvStoreCompeteActivity.this, DatabaseUtil.TYPE_COMPETE , Integer.parseInt(mVisitTaskInfo.mVisitId), 1);
            }else{
            	DatabaseUtil.saveVisitStatus(SvStoreCompeteActivity.this, DatabaseUtil.TYPE_COMPETE , Integer.parseInt(mVisitTaskInfo.mVisitId), 0);
            }
            saveSelectData();
            return null;
        }

        protected void onPostExecute(Void result) { //保存完毕
            dismissDialog(SAVE_DIALOG);
            Actions.toastShort(SvStoreCompeteActivity.this, getString(R.string.datasave_success));
        }
    }

    /**
	 * 更新project数据从数据库
	 * @param cursor 数据库数据
	 */
	private void updateProjectDataFromCursor(Cursor cursor){
		mProjectList = DatabaseUtil.getProjectDataList(cursor);
		if(mProjectList != null && mProjectList.size() > 0){
			for(ProjectDataItem item : mProjectList){
				updateViewFromProject(item);
			}
		}
		startQueryDataItemList();
	}

    /**
	 * 从project数据更新界面
	 */
	private void updateViewFromProject(ProjectDataItem item) {
		if (item.mId == 1) {
			if (item.isShow) {
				mCompeteProductsView.setLableText(item.mName);
			} else {
				mCompeteProductsView.setVisibility(View.GONE);
			}
		} else if (item.mId == 2) {
			if (item.isShow) {
				mNewProductsView.setLableText(item.mName);
			} else {
				mNewProductsView.setVisibility(View.GONE);
			}
		} else if (item.mId == 3) {
			if (item.isShow) {
				mPromotionsView.setLableText(item.mName);
			} else {
				mPromotionsView.setVisibility(View.GONE);
			}
		}
	}

	/**
	 * 从管理员查询结果数据更新界面
	 */
	private void setDataFromAdminSearchData() {
		findViewById(R.id.svbottom_split_menu).setVisibility(View.GONE);
		findViewById(R.id.svbottom_menu_save).setVisibility(View.GONE);
		if (DataUtils.mQueryVisitContentResult.Data != null) {
			List<ProjectDataItem> list = DataUtils
					.getProjectDataList(StoresManagerSettings.DataProjectsColumns.PROJECT_TYPE_COMPETE);
			if (list != null && list.size() > 0) {
				for (ProjectDataItem item : list) {
					updateViewFromProject(item);
				}
			}
			if(DataUtils.mQueryVisitContentResult.Data.CompPrices != null){ //竞品产品价格
				for(SyncModel.DataVisitContentCompetitor content : DataUtils.mQueryVisitContentResult.Data.CompPrices){
					CompeteProductItem item = new CompeteProductItem();
					item.mType = TYPE_COMPETE_CONTENT_PRICE;
					item.copyFrom(content);
					 mCompeteProductsView.addCompeteProductData(item, false);
				}
			}
		    if(DataUtils.mQueryVisitContentResult.Data.CompNews != null){ //新品上市信息
				for(SyncModel.DataVisitContentCompetitor content : DataUtils.mQueryVisitContentResult.Data.CompNews){
					CompeteProductItem item = new CompeteProductItem();
					item.mType = TYPE_COMPETE_CONTENT_FUCTION;
					item.copyFrom(content);
					mNewProductsView.addNewProductData(item);
				}
			}
		    if(DataUtils.mQueryVisitContentResult.Data.CompPromotions != null){ //竞品促销活动
				for(SyncModel.DataVisitContentCompetitor content : DataUtils.mQueryVisitContentResult.Data.CompPromotions){
					CompeteProductItem item = new CompeteProductItem();
					item.mType = TYPE_COMPETE_CONTENT_ACTIVITY;
					item.copyFrom(content);
					mPromotionsView.addNewProductData(item);
				}
			}
		}
	}

	/**
	 * 保存选择数据
	 */
	private void saveSelectData(){
		ContentValues values = new ContentValues();
		values.put(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.PRICE, mCompeteProductsView.isSelect() ? 1 : 0);
		values.put(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.NEW, mNewProductsView.isSelect() ? 1 : 0);
		values.put(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.ACTIVITY, mPromotionsView.isSelect() ? 1 : 0);
		values.put(StoresManagerSettings.SAVED, 0);
		if(mSelectedBdId == -1){
			values.put(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.STOREID, mVisitTaskInfo.mStoreId);
			getContentResolver().insert(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.CONTENT_URI_NO_NOTIFICATION, values);
		}else{
			getContentResolver().update(StoresManagerSettings.SupervisorStoreCompeteSelectedColumns.getContentUri(mSelectedBdId, false), values, null, null);
		}
	}
	/**
     * 退出提示
     * 
     */
    private void popExitDialog() {
    	Actions.popExitDialog(this);
    }
}
